<?php
header("Content-Type: text/html; charset=iso-8859-1");
session_start();
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];
$fsize = $_FILES['image']['size'];
$ftype = $_FILES['image']['type'];
$equipe = (!empty($_SESSION['equipe'])) ? $_SESSION['equipe'] : "";
$types = array(
	1=>"gif",
	2=>"jpeg",
	3=>"png",
	4=>"swf",
	5=>"psd",
	6=>"bmp"
	);
if(IsSet($ftmp) && $fsize) :
 	$list_files = array();
    $dir = "photos/equipes/";
    if ($handle_folder = opendir($dir)) {
       while (false !== ($file = readdir($handle_folder))) {
           if($file != "." && $file != ".." && !strstr($file,"__MACOSX")){
				$ancien = (date('m') > 07) ?  sprintf("%02s",date('y')-1)."-".date('y') : sprintf("%02s",date('y')-2)."-".sprintf("%02s",date('y')-1);
				if($file == $equipe."-grand.jpg") rename($dir.$file,$dir.$equipe."_".$ancien.".jpg");
				elseif($file == $equipe.".jpg") unlink($dir.$file);
			} 
       }
       closedir($handle_folder);
    }
	$type = $types[exif_imagetype($ftmp)];
	$fname = $equipe."-grand.jpg";
	$newname = $equipe.".jpg";
	if(move_uploaded_file($ftmp,$dir.$fname)){
		chmod($dir.$fname,0644);
		$filename = $fname;
		list($width, $height) = getimagesize($dir.$filename);
		$new_width = 260;
		$new_height = $height * 260 / $width;
		$image_p = imagecreatetruecolor($new_width, $new_height);
		if($type == "jpeg"){
			$image = imagecreatefromjpeg($dir.$filename);
			imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			imagejpeg($image_p,$dir.$newname, 100);
		}
	}
	$tag = $equipe;
	echo "<html><head>\n";
	echo "<script type = 'text/javascript'>\n";
	echo "var par = window.parent.document;\n";
	echo "var list = par.getElementById('photo');\n";
	echo "list.src = '$dir/$newname';\n";
	echo "</script>\n";
	echo "</head>\n";
	echo "</html>\n";
	
    exit();
endif;
?>
<html><head>
<script language="javascript">
function upload(){
// hide old iframe
    var par = window.parent.document;
    var num = par.getElementsByTagName('iframe').length - 1;
    var iframe = par.getElementsByTagName('iframe')[num];
    iframe.className = 'hidden';

    // create new iframe
    var new_iframe = par.createElement('iframe');
    new_iframe.src = 'photo_equipe_upload.php';
    new_iframe.frameBorder = '0';
    par.getElementById('iframe').appendChild(new_iframe);

    // add image progress
    // var list = par.getElementById('corps');
    // var new_div = par.createElement('div');
    // var new_img = par.createElement('img');
    // new_img.src = 'images/icon_indicator.gif';
    // new_img.className = 'load';
    // new_div.appendChild(new_img);
    // list.appendChild(new_div);
    // 
    // // send
    // var imgnum = list.getElementsByTagName('div').length - 1;
    // document.iform.imgnum.value = imgnum;
    document.iform.submit();
}
</script>
<style>
body {vertical-align:top;}
</style>
<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />
</head>
<body style = 'background-color: #fffacd'>
	<div id = 'corps'>
<p class = 'center bold'>Ajouter une photo</p>
<form name="iform" action="" method="post" enctype="multipart/form-data">
<p class = 'center'><input id="file" type="file" name="image" size = '10' onchange="upload()" />
<input type="hidden" name="imgnum" value = '1'/></p>
</form>
</div>
</body>
</html>